from Common.connect_to_device import *
from importlib import import_module

def get_locator_class(model: str, page_type: str):
    # 从配置中查找对应车型和页面类型的类路径
    class_path = MODEL_VERSIONS.get(model, {}).get(page_type)

    # 如果找不到车型对应的适配类，自动退回使用 base 类
    if not class_path:
        print(f"未找到 {model} 模型的 {page_type} 页面适配类，使用 base 页面")
        class_path = MODEL_VERSIONS.get("Base", {}).get(page_type)

    if not class_path:
        raise ValueError(f"Base 模型的 {page_type} 页面的适配类也未配置")

    # 模块名 + 类名
    module_path, class_name = class_path.rsplit(".", 1)

    # 动态导入模块并获取类
    module = import_module(module_path)
    locator_class = getattr(module, class_name)

    return locator_class

MODEL_VERSIONS = {
    "Base": {"main": "PageLocators.Arc_Base_locator.Arc_Base_Locator"},
    "BeiJing": {"main": "PageLocators.BeiJing_IQI_locator.BeiJingIQYLocator"},
    "CX1E": {"main": "PageLocators.Other_locator.CX1E_Locator"},
    "LanTu": {"main": "PageLocators.LanTu_locator.LanTu_Locator"},
    "WuLing": {"main": "PageLocators.Other_locator.WuLing_Locator"},
    "TongYong": {"main": "PageLocators.Other_locator.TongYong_Locator"},
}
loc=get_locator_class(model=app_mode,page_type="main")